package com.sensePlusVR.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.DensityUtil;
import com.sensePlusVR.utils.MyLog;

import java.text.NumberFormat;

@SuppressLint("HandlerLeak")
@SuppressWarnings("unused")
public class WaterRippleHelper {
	  private static final String TAG = WaterRippleHelper.class.getSimpleName();
	  private Context context;
	  private View rootView;
	  
	  private View rootWrvView;
	  private WaterRippleView waterRippleView;
	  private ImageView rotation_iv;
	  private TextView percent_tv;
	  private TextView detect_test_tip1;
	  private TextView detect_test_start_tv;
	  private RelativeLayout detect_detail_relative;
	  private Button detect_enter_btn;
	  
	  private static final int MOVE_DURATION = 500;
	  
	
	  private int currentCount = 0;
	  private int sum = 0;
	 
	  private int startCount = 0;
	  private int endCount =0;
	  private boolean isContinue = false;
	  private boolean isNewStart = false;
	  
	  private int mDuration = 0;
	  
	  public WaterRippleHelper(Context context,View rootView){
	    this.context = context;
	    this.rootView = rootView;
	    
	    rootWrvView = rootView.findViewById(R.id.detect_watterripple_fram);
	    waterRippleView = (WaterRippleView) rootView.findViewById(R.id.waterRippleView);
	    rotation_iv = (ImageView) rootView.findViewById(R.id.rotation_iv);
	    percent_tv = (TextView) rootView.findViewById(R.id.percent_tv);
	    detect_test_tip1 = (TextView) rootView.findViewById(R.id.detect_test_tip1);
	    detect_test_start_tv = (TextView) rootView.findViewById(R.id.detect_test_start_tv);
	    detect_detail_relative = (RelativeLayout) rootView.findViewById(R.id.detect_detail_relative);
	    detect_enter_btn = (Button) rootView.findViewById(R.id.detect_enter_btn);
	    
	    Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/nowthin.otf");  
	    percent_tv.setTypeface(typeFace); 
	  }
	  
	  Handler handler = new Handler(){
	    @Override public void handleMessage(Message msg) {
	      super.handleMessage(msg);
	      Log.d(TAG, "currentCount : " + currentCount + " startCount : " + startCount + " endCount : " + endCount);
	      if (currentCount == 0 || isContinue){
	    	currentCount = startCount;
	        waterRippleView.setDepthRate((float)(endCount - startCount)/sum);
	        if(isNewStart){
	        	waterRippleView.setDuration(10);
	        }
	      //计算设置跟新Textview百分比,要和水波保持一致
	    	mDuration = (int) ((waterRippleView.getDuration()) * ((float)(endCount - startCount)/sum)/((endCount - startCount)/3));
	        if(currentCount == 0){
	        	 waterRippleView.startRefresh(false);
	        }else{
	        	 waterRippleView.startRefresh(true);
	        }
	        waterRippleView.startRefresh2();
	        isContinue = false;
	      }
	      if(currentCount<endCount){
	    	  currentCount+=3;
	    	  Log.d(TAG, "mDuration : " + mDuration);
	        handler.sendEmptyMessageDelayed(0, mDuration);
	      }else if(currentCount>endCount) {
	    	  currentCount=endCount;
	      }
	      NumberFormat numberFormat = NumberFormat.getInstance();
	      numberFormat.setMaximumFractionDigits(0);
	      if(currentCount > sum)return;
	      String result = numberFormat.format((float) currentCount / (float) sum * 100);
	      if(isNewStart){
	    	  result = "60";
	      }
	      percent_tv.setText(result+"%");
	    }
	  };

	  /*
	  * root 根布局
	  * tag 点击控件的tag
	  */
	  public void showEvent(int startCount,int endCount,int sum,boolean isContinue,boolean isNewStart){
		this.startCount = startCount;
	    this.endCount = endCount;
	    this.sum = sum;
	    this.isContinue = isContinue;
	    this.isNewStart = isNewStart;
	    MyLog.d(TAG, "isContinue : " + isContinue + " isNewStart : " + isNewStart);
	      rootWrvView.postDelayed(new Runnable() {
	        @Override public void run() {
	          handler.sendEmptyMessage(0);
	        }
	      },200);
	  }
	  
	  
	  public void startAnim(){
		  //开启外圈旋转动画
		  Animation animation = AnimationUtils.loadAnimation(context,
				R.anim.detect_outer_rotation);
		LinearInterpolator lin = new LinearInterpolator();// 设置动画匀速运动
		animation.setInterpolator(lin);
		rotation_iv.setVisibility(View.VISIBLE);
		rotation_iv.startAnimation(animation);
		
		//水波动画 
	      rootWrvView.setAlpha(0);
	      rootWrvView.setTranslationX(-50f);
	      rootWrvView.setTranslationY(-50f);
	      rootWrvView.setScaleX(0.1f);
	      rootWrvView.setScaleY(0.1f);
	      ViewCompat.animate(rootWrvView)
	          .setDuration(500)
	          .setInterpolator(new DecelerateInterpolator(1.2f))
	          .scaleX(1f)
	          .scaleY(1f)
	          .alpha(1f)
	          .translationX(0)
	          .translationY(0)
	          .start();
	  }
	  
	  public void clearAnim(){
		  rotation_iv.clearAnimation();
	  }
	  
	  
	  public void moveView(){
		  int circleMoveY = DensityUtil.dip2px(context, 130);
		  MyLog.d(TAG, "circleMoveY ：" + circleMoveY);
		  ViewCompat.animate((View)rootWrvView.getParent())
		  .translationX(0)
		  .translationY(-circleMoveY)
          .setStartDelay(0)
          .scaleX(0.6f)
          .scaleY(0.6f)
          .setUpdateListener(null)
          .setInterpolator(new AccelerateDecelerateInterpolator())
          .setDuration(MOVE_DURATION)
          .withEndAction(new Runnable() {
              @Override
              public void run() {

              }
          })
          .start();
		  
		  ViewCompat.animate(detect_test_start_tv)
		  .translationX(0)
		  .translationY(0)
          .setStartDelay(0)
          .scaleX(0.0f)
          .scaleY(0.0f)
          .alpha(0.0f)
          .setUpdateListener(null)
          .setInterpolator(new AccelerateDecelerateInterpolator())
          .setDuration(MOVE_DURATION)
          .withEndAction(new Runnable() {
              @Override
              public void run() {

              }
          })
          .start();
		  
		  ViewCompat.animate(detect_test_tip1)
		  .translationX(0)
		  .translationY(0)
          .setStartDelay(0)
          .scaleX(0.0f)
          .scaleY(0.0f)
          .alpha(0.0f)
          .setUpdateListener(null)
          .setInterpolator(new AccelerateDecelerateInterpolator())
          .setDuration(MOVE_DURATION)
          .withEndAction(new Runnable() {
              @Override
              public void run() {

              }
          })
          .start();
		  
		  
		AnimationSet animationSet = new AnimationSet(true);
		// 相对x,y如果移动到当前xml位置则为0,此处代表从屏幕外移动到当前位置
		TranslateAnimation translateAnimation = new TranslateAnimation(0, 0,
				DensityUtil.getScreenHeight(context), 0);
		translateAnimation.setDuration(MOVE_DURATION);
		translateAnimation.setFillAfter(true);
		translateAnimation.setInterpolator(new LinearInterpolator());

		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(MOVE_DURATION);
		alphaAnimation.setFillAfter(true);
		alphaAnimation.setInterpolator(new LinearInterpolator());

		animationSet.addAnimation(translateAnimation);
		animationSet.addAnimation(alphaAnimation);

		detect_detail_relative.startAnimation(animationSet);
		detect_enter_btn.startAnimation(animationSet);

		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				detect_detail_relative.setVisibility(View.VISIBLE);
				detect_enter_btn.setVisibility(View.VISIBLE);
			}
		});
		  
		  //停止外圈旋转动画
		  rotation_iv.clearAnimation();
		  rotation_iv.setImageResource(R.drawable.loading_oval_default);
		  
	  }
}
